﻿@using System.Globalization
@{
    ViewBag.Title = "过滤器配置";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        .add
        {
            display: none;
            width: 200px;
        }
        .addColumn select
        {
            width: 128px;
        }
        .searchArea
        {
            width: 590px;
        }
        .listTitle
        {
            width: 590px;
        }
        .viewTb
        {
            height: 270px;
            padding-right: 3px;
            overflow-y: scroll;
        }
        .tr1
        {
            display: none;
        }
        .version, .modelEdit, .tbExpression, .versionType
        {
            display: none;
        }
    </style>
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        var sysArr = new Array();
        var viewArr = {};
        var OperatorList = new Array();
        var IsModel = 0;
        $(function () {
            $("#loading").show();
            var parent = window.dialogArguments; //获取父页面 
            var id = UrlArray["id"];
            if (UrlArray["type"] == "4") {
                if (id != "") {
                    IsModel = parent.retData[id].ColumnIsModel;
                    $("input:radio[value=" + parent.retData[id].WhereCondition + "]").attr("checked", "checked");
                }
                if (UrlArray["version"]) {
                    $(".version").show();
                    $(".tbExpression .button").hide();
                    $(".versionWhere").html(parent.retData[id].WhereCondition);
                    $(".tbExpression").empty();
                    if (!jv.IsNull(parent.retData[id].WhereCondition)) {
                        var parentOpts = parent.retData[id].Operator.split("&");
                        var parentExps = parent.retData[id].ColumnExpression.split("&");
                        for (var i = 0; i < parentOpts.length; i++) {
                            $(".tbExpression").append(parentOpts[i] + parentExps[i] + "<br/>");
                        }
                        $(".tbExpression").show();
                        $("#loading").hide();
                    }
                } else {
                    $(".modelEdit").show();
                    $.post("/DQConfig/GetOperatorList", function (result) {
                        for (var i = 0; i < result.length; i++) {
                            OperatorList.push("<option>" + result[i].LegalOperator + "</option>");
                        }
                        if (id != "") {
                            //初始化已配置表达式
                            if (parent.retData[id].Operator) {
                                var parentOpts = parent.retData[id].Operator.split("&");
                                var parentExps = parent.retData[id].ColumnExpression.split("&");
                                var parentIsValue = parent.retData[id].IsValue.split("&");
                                for (var i = 0; i < parentOpts.length; i++) {
                                    AddCondition(parentOpts[i], parentExps[i], parentIsValue[i]);
                                }
                                $(".tbExpression").show();
                                $("#loading").hide();
                            } else {
                                $.post("/ModelConfig/GetColumnExpression", { columnid: parent.retData[id].GUID, dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                                    if (result.length > 0) {
                                        for (var i = 0; i < result.length; i++) {
                                            AddCondition(result[i].ColumnOperator, result[i].ColumnExpression, result[i].IsValue);
                                        }
                                    }
                                    $(".tbExpression").show();
                                    $("#loading").hide();
                                });
                            }
                        }
                    });
                }
            }
        });
        function save() {
            var toSave = true;
            if (IsModel == 1 && UrlArray["type"] == "2") {
                toSave = confirm("该字段为模型初始化字段，点击确定将修改为模型扩展字段。");
                if (toSave)
                    IsModel = 0;
            }
            var operatorValue = getArrValue(".opts", "&");
            var columnExpressionVlaue = getArrValue(".exps", "&");
            var columnIsValue = getRadioArr(".IsValue").join("&"); 
            //            if (operatorValue == "") {
            //                alert("过滤器不能为空。");
            //                toSave = false;
            //            }
            if (toSave) {
                var retArr = {};
                retArr["GUID"] = UrlArray["id"];
                retArr["ColumnIsModel"] = IsModel;
                retArr["WhereCondition"] = $("input[name='WhereCondition']:checked").val();
                retArr["Operator"] = operatorValue;
                retArr["ColumnExpression"] = columnExpressionVlaue;
                retArr["IsValue"] = columnIsValue;
                returnParent(retArr);
                return false;
            }
        }
        var cdNum = 0;
        function AddCondition(opt, exp, isvalue) {
            var optcontent = OperatorList.join("");
            optcontent = optcontent.replace("<option>" + opt + "</option>", "<option selected>" + opt + "</option>");
            var isv = "";
            var iss = "checked='checked'";
            if (isvalue == "1") {
                isv = "checked='checked'";
                iss = "";
            }
            var addHtml = "<div class='addcd" + cdNum + "'><select class='sel_Operator" + cdNum + " opts' style='width: 90px'>" + optcontent
             + "</select> <input type='text' value='" + exp + "' maxlength='450' class='txt_ColumnExpression" + cdNum + " exps' style='width: 190px;' /> <input type='radio' name='IsValue" +
                cdNum + "' value='1' " + isv + " class='IsValue'/>值</lable> <lable><input type='radio' name='IsValue" + cdNum + "' value='0' " + iss + " class='IsValue' />函数 &nbsp; <input type='button' class='button' value='删除' onclick='DelCondition(" + cdNum + ");' /></div>";
            $(".tbExpression").append(addHtml);
            cdNum++;
        }
        function DelCondition(v) {
            $(".addcd" + v).remove();
        }
    </script>
}
<div style="margin: 5px;" class="viewTb">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="600">
                <div class="version">
                    查看模型配置版本记录 -> 模型扩展字段 -> 过滤器
                </div>
                <div class="modelEdit">
                    模型扩展字段配置 -> 编辑字段信息</div>
            </td>
            <td align="right">
                <div class="modelEdit">
                    <input type="button" class="button" value="完成" onclick="save()" />
                </div>
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td valign="top" height="220">
                <table border="0" class="table_bg" cellpadding="0" cellspacing="0" width="100%">
                    <tr class="4">
                        <td align="right" width="80">
                            过滤器关系：
                        </td>
                        <td>
                            <div class="versionWhere">
                            </div>
                            <div class="modelEdit">
                                <input type="radio" name="WhereCondition" value="AND" checked="checked" />
                                AND
                                <input type="radio" name="WhereCondition" value="OR" />
                                OR
                            </div>
                        </td>
                    </tr>
                    <tr class="4">
                        <td align="right" valign="top">
                            过滤器：
                        </td>
                        <td class="tbExpression">
                            <div style="text-align: right;">
                                <input type="button" class="button" value="添加" onclick="AddCondition('','','1');" />
                            </div>
                        </td>
                    </tr>
                </table>
                <div id="loading" class="loading">
                    <img src="/Images/tree_loading.gif" />
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="versionType" style="margin: 5px; height: 270px; overflow-y: scroll;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="600">
                查看模型配置版本记录 -> 模型扩展字段 -> 脚本
            </td>
            <td align="right">
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td valign="top" height="220">
                <div class="scriptText">
                </div>
            </td>
        </tr>
    </table>
</div>
